﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace Code
{
    public class JsonHelp
    {
        static DataContractJsonSerializerSettings st = new DataContractJsonSerializerSettings() { DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("yyyy-MM-ddTHH:mm:ss") };

        /// <summary>
        /// 系统序列化 JSON
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string ObjectToJson(object item)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType(), st);
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, item);
                StringBuilder sb = new StringBuilder();
                sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
                return sb.ToString();
            }
        }

        /// <summary>
        /// 反序列化到实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string jsonString)
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T), st);
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T jsonObject = (T)ser.ReadObject(ms);
            ms.Close();
            return jsonObject;
        }

        /// <summary>
        /// 反序列化到实体类 优化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonToModel<T>(string jsonString)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                return (T)new DataContractJsonSerializer(typeof(T),st).ReadObject(ms);
            }
        }

        /// <summary>
        /// 系统序列化 JSON 优化
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <returns></returns>
        public static string ModelToJson(object jsonObject)
        {
            using (var ms = new MemoryStream())
            {
                new DataContractJsonSerializer(jsonObject.GetType(), st).WriteObject(ms, jsonObject);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        public static  System.Web.Script.Serialization.JavaScriptSerializer JavaScriptJson = new  System.Web.Script.Serialization.JavaScriptSerializer();

        /// <summary>
        /// (系统web方式) 系统序列化 JSON
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <returns></returns>
        public static string ModelToJavaScriptJson(object jsonObject)
        {
            
            string str=JavaScriptJson.Serialize(jsonObject);
            //str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match =>
            //{
            //    DateTime dt = new DateTime(1970, 1, 1);
            //    dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
            //    dt = dt.ToLocalTime();
            //    return dt.ToString("yyyy-MM-ddTHH:mm:ss");
            //});
            return str;
        }

        /// <summary>
        /// (系统web方式) 反序列化到实体类 
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <returns></returns>
        public static T JsonToJavaScriptModel<T>(string jsonString)
        {
            return JavaScriptJson.Deserialize<T>(jsonString);
        }
    }

}
